<template>
	<view class="order-list">

		<view class='list-box' :style="'height:'+( windowHeight)  + 'px'">
			<scroll-view :style="'height: '+(windowHeight-110) + 'px;'" scroll-y="true" class="scroll-Y">
				<view style=" height: 100%;">
					<view class="list-item">
						<block v-for="(item1, index1) in pfMallOrderProductsItemList" :key="index1">
							<view class="list-cell">
								<image class="img" mode="aspectFill"
									:src="item1.imgUrl||'https://dnsverify.pfmoto.com/appstatic/img/my/appoint_img_small.png'" />
								<view class="list-content_text" style="width: 426rpx;">
									<view class="layout-1" style="justify-content: space-between;">
										<text class="text-5"
											style="width: 300rpx;text-align: left;">{{item1.productName}}</text>
										<text class="text-5">X{{item1.count}}</text>
									</view>
									<view class="layout-1" style="margin-top: 4px;">
										<text class="text-1">{{item1.attValues}}</text>
									</view>
									<view class="layout-1" style="margin-top: 4px;justify-content:flex-end;">
										<text class="text-3" style="margin-top: 4px;"
											v-if="item1.payType=='3'">￥{{item1.mixturePrice}}+{{item1.mixturePoint}}分</text>
										<text class="text-3" style="margin-top: 4px;"
											v-else-if="item1.payType=='2'">{{item1.point}}分</text>
										<text class="text-3" style="margin-top: 4px;" v-else>￥{{item1.price}}</text>
									</view>
									<view class="layout-1" style="margin-top: 4px;justify-content:flex-end;">
										<text class="text-5" style='margin-right: 10px;'>实付</text>
										<text class="text-3" v-if="item1.payType=='1'">￥{{item1.finalPrice}}</text>
										<text class="text-3" v-else-if="item1.payType=='2'">{{item1.finalPoint}}分</text>
										<text class="text-3"
											v-else>￥{{item1.finalDiscountMixturePrice}}+{{item1.finalDiscountMixturePoint}}分</text>
									</view>
								</view>
							</view>
						</block>


						<block v-if="currentStatus=='1'">
							<!-- ||currentStatus=='2' -->
							<view class="info-transverse">
								<view class="text-7">商品总价</view>
								<view class="text-8">￥{{data.orderAmount}}</view>
							</view>
							<view class="info-transverse">
								<view class="text-7">商品总积分</view>
								<view class="text-8">{{data.orderScore}}</view>
							</view>
							<view class="info-transverse">
								<view class="text-7">运费</view>
								<view class="text-8">￥{{data.freightPrice}}</view>
							</view>
							<view class="info-transverse">
								<view class="text-7">优惠券</view>
								<view class="text-8">无</view>
							</view>
							<view class="info-transverse">
								<view class="text-7">派币</view>
								<view class="text-8">无</view>
							</view>
							<view class="info-transverse">
								<view class="text-7" v-if="currentStatus==1">待支付</view>
								<view class="text-7" v-else>实付</view>
								<text class="text-8" v-if="data.orderScore!=0"
									style="color: #EA5404;">￥{{data.orderAmount||0}}+{{data.orderScore}}分</text>
								<text class="text-8" v-else style="color: #EA5404;">￥{{data.orderAmount||0}}</text>
							</view>
						</block>

					</view>

					<view class="list-item" style="margin-top: 28rpx;">
						<view class="info-transverse" v-if="currentStatus=='1'">
							<view class="text-5">取消原因</view>
							<view class="layout-personal-info" @click="showCancelResonPicker=true">
								<view class="text-5" style="margin-right: 8rpx;" v-if="cancelReson==''">请选择</view>
								<view class="text-5" style="margin-right: 8rpx;" v-else>{{cancelReson}}</view>
								<u-icon name="arrow-right" color="#ADB0AE" size="12"></u-icon>
							</view>
						</view>
						<block v-if="currentStatus!='1'">
							<view class="info-transverse">
								<view class="text-5">退款方式</view>
								<view class="layout-personal-info" @click="showRefundTypePicker=true">
									<view class="text-5" style="margin-right: 8rpx;" v-if="refundTypeName==''">请选择
									</view>
									<view class="text-5" style="margin-right: 8rpx;" v-else>{{refundTypeName}}</view>
									<u-icon name="arrow-right" color="#ADB0AE" size="12"></u-icon>
								</view>
							</view>

							<view class="info-transverse">
								<view class="text-5">退货原因</view>
								<view class="layout-personal-info" @click="showRefundResonPicker=true">
									<view class="text-5" style="margin-right: 8rpx;" v-if="refundReason==''">请选择</view>
									<view class="text-5" style="margin-right: 8rpx;" v-else>{{refundReason}}</view>
									<u-icon name="arrow-right" color="#ADB0AE" size="12"></u-icon>
								</view>
							</view>
							<view class="info-transverse">
								<view class="text-5">退货数量</view>
								<u-number-box button-size="36" color="#3D3A3A" bgColor="#EDEDED"
									iconStyle="color: #3D3A3A" @change="valChange" :max="refundableNum" disabledInput>
								</u-number-box>
							</view>


							<view class="info-transverse">
								<view class="text-5">退款金额</view>
								<u--input type="number" value='refundMoney' v-model='refundMoney'
									data-type='refundMoney' @input='inputValueChange' placeholder="请提前和商家协商沟通"
									placeholder-style="color:#ADB0AE" inputAlign='right' border='none' fontSize='24rpx'>
								</u--input>
							</view>
							<view class="info-transverse">
								<view class="text-5">退还积分</view>
								<view class="uni-text">{{refundScore}}</view>
							</view>
						</block>
					</view>
					<view class="list-item" style="margin-top: 28rpx;" v-if="currentStatus!='1'">
						<!-- &&currentStatus!='2' -->
						<textarea class="uni-textarea" type="text" value='cancelResonText' v-model='cancelResonText'
							data-type='cancelResonText' maxlength="150" ref="textarea" placeholder="请输入具体原因"
							placeholder-style="color:#ADB0AE" auto-height />
						<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" multiple :maxCount="5"
							style='margin-left: 32rpx;margin-bottom: 32rpx;margin-top: 32rpx;'>
						</u-upload>
					</view>

				</view>
			</scroll-view>

			<view class="cancelBtns">
				<text v-if="currentStatus=='1'" class="evaluateBtn" @click="cancelOrderRefund">确定</text>
				<view class="layout-3" v-else>
					<!-- v-else-if="currentStatus=='2'||currentStatus=='3'||currentStatus=='4'||currentStatus=='5'" -->
					<text class="text-cancel" @click="cancelRefundBtn">取消</text>
					<text v-if="isUploading" class="text-cancel" style="margin-left: 20px;">提交</text>
					<text v-if="!isUploading" class="text-evaluate" style="margin-left: 20px;" @click="orderRefund">提交</text>
				</view>
			</view>
		</view>
		<u-picker :show='showCancelResonPicker' :columns="cancelResonList" keyName="contentName"
			@cancel="showCancelResonPicker=false" @confirm="confirmPicker" confirmColor='#EA5404'>
		</u-picker>
		<u-picker :show='showRefundResonPicker' :columns="refundResonList" keyName="contentName"
			@cancel="showRefundResonPicker=false" @confirm="confirmPicker" confirmColor='#EA5404'>
		</u-picker>
		
		
		<u-picker :show='showRefundTypePicker' :columns="refundTypeList" keyName="refundTypeName"
			@cancel="showRefundTypePicker=false" @confirm="confirmPicker" confirmColor='#EA5404'>
		</u-picker>
	</view>
</template>
<script>
	const app = getApp();
	const util = require('@/utils/util.js');
	const network = require('@/manager/network-manager.js');
	export default {
		data() {
			return {
				showMoneyText: true,
				windowHeight: app.globalData.windowHeight,
				navigationBarHeight: app.globalData.navigationBarHeight,
				contentHeight: app.globalData.windowHeight - 100,
				// type: 1, //1 取消订单 2 未发货退款 3 已发货退款  
				currentStatus: '',
				orderNo: '',
				refundNum: 1,
				refundMoney: '',
				refundScore: '',
				pfMallOrderProductsItemList: [],

				showCancelResonPicker: false,
				cancelReson: '',
				contentCode: '',

				cancelResonList: [],
				
				
				showRefundResonPicker: false,
				refundReason: '',
				refundCode: '',
				refundResonList: [],
				
				
				showRefundTypePicker: false,
				refundTypeName: '',
				refundType: '',
				refundTypeList: [
					[{
						refundTypeName: '仅退款',
						refundType: '0'
					}, {
						refundTypeName: '退货退款',
						refundType: '1'
					}]
				],


				cancelResonText: '',
				fileList: [],
				data: {},
				from: '',
				refundableNum: 0,

				isUploading:false,
			}
		},
		onLoad: function(options) {
			debugger
			this.currentStatus = options.currentStatus
			this.orderNo = options.orderNo
			if (this.currentStatus != 1) {
				this.pfMallOrderProductsItemList = JSON.parse(options.pfMallOrderProductsItemListJson)
				if (this.pfMallOrderProductsItemList[0].payType == 1) {
					this.refundMoney = this.pfMallOrderProductsItemList[0].finalPrice * this.refundNum
					this.refundScore = 0

				} else if (this.pfMallOrderProductsItemList[0].payType == 2) {
					this.refundMoney = 0
					this.refundScore = this.pfMallOrderProductsItemList[0].finalPoint * this.refundNum
				} else if (this.pfMallOrderProductsItemList[0].payType == 3) {
					this.refundMoney = this.pfMallOrderProductsItemList[0].finalDiscountMixturePrice * this.refundNum
					this.refundScore = this.pfMallOrderProductsItemList[0].finalDiscountMixturePoint * this.refundNum
				}
				if (options.from) {
					this.from = options.from
					this.refundableNum = options.refundableNum
				} else {
					this.refundableNum = this.pfMallOrderProductsItemList[0].count
				}
			} else {
				this.getProductOrderDetailsByOrderNo()
			}



			this.getCancelReasonList()
			this.getReturnReasonList()
		},


		methods: {
			//计数器数量变化
			valChange: function(e) {
				this.refundNum = e.value
				if (this.pfMallOrderProductsItemList[0].payType == 1) {
					this.refundMoney = this.pfMallOrderProductsItemList[0].finalPrice * this.refundNum
					this.refundScore = 0
				} else if (this.pfMallOrderProductsItemList[0].payType == 2) {
					this.refundMoney = 0
					this.refundScore = this.pfMallOrderProductsItemList[0].finalPoint * this.refundNum
				} else if (this.pfMallOrderProductsItemList[0].payType == 3) {
					this.refundMoney = this.pfMallOrderProductsItemList[0].finalDiscountMixturePrice * this.refundNum
					this.refundScore = this.pfMallOrderProductsItemList[0].finalDiscountMixturePoint * this.refundNum
				}
			},


			//提交退款
			cancelOrderRefund: function() {
				var that = this
				if (that.cancelReson == '') {
					uni.showToast({
						title: '请选择取消原因',
						duration: 3000,
						icon: 'none'
					})
				} else {
					that.cancelOrder()
				}
			},

			//确定--取消订单
			cancelOrder: function() {

				var that = this
				var url = app.globalData.config.interfaces.URL_PRODUCT_PFMALLORDER_CANCELORDER
				var params = {
					orderNo: that.orderNo,
					cancelRemarkCode: that.contentCode
				};
				network.get(url, params, 'form', null, true).then(res => {
					if (res.code == 200) {
						uni.navigateBack({
							delta: 1
						})
					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
				});
			},
			//提交退款
			orderRefund: function() {
				var that = this
				if (that.refundType == '') {
					uni.showToast({
						title: '请选择退款方式',
						duration: 3000,
						icon: 'none'
					})
				} else if (that.refundReason == '') {
					uni.showToast({
						title: '请选择退款原因',
						duration: 3000,
						icon: 'none'
					})
				} else if (that.cancelResonText == '') {
					uni.showToast({
						title: '请输入具体退款原因',
						duration: 3000,
						icon: 'none'
					})
				} else {
					that.orderRefundApplication()
				}
			},
			//提交退款申请
			orderRefundApplication: function() {
				var that = this
				var refundImages = []
				for (var i = 0; i < that.fileList.length; i++) {
					refundImages.push(that.fileList[i].url)
				}
				var url = app.globalData.config.interfaces.URL_PRODUCT_PFMALLORDERCANCELAPPLY_ORDERREFUUNDAPPLICATION
				var params = {
					orderId: that.pfMallOrderProductsItemList[0].orderId,
					// productItemId: that.pfMallOrderProductsItemList[0].id,
					refundDesc: that.cancelResonText,
					refundImages: refundImages,
					refundMoney: that.refundMoney,
					refundNum: that.refundNum,
					refundReason: that.refundReason,
					refundScore: that.refundScore,
					refundType: that.refundType,
					userId: app.getUserId()
				};
				
				
				if(that.from){
					params.productItemId = that.pfMallOrderProductsItemList[0].itemId
				}else{
					params.productItemId = that.pfMallOrderProductsItemList[0].id
				}
				network.post(url, params, 'json', null, true).then(res => {
					if (res.code == 200) {
						uni.navigateBack({
							delta: 1
						})
					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
				});

			},
			//取消--取消退款返回上一页
			cancelRefundBtn: function() {
				uni.navigateBack({
					delta: 1
				})
			},
			//确定
			confirmPicker(e) {
				// 退款/取消原因
				if (this.showCancelResonPicker == true) {
					this.showCancelResonPicker = false
					this.cancelReson = e.value[0].contentName
					this.contentCode = e.value[0].contentCode
				}
				//退款方式
				if (this.showRefundTypePicker == true) {
					this.showRefundTypePicker = false
					this.refundTypeName = e.value[0].refundTypeName
					this.refundType = e.value[0].refundType
				}
				//退货原因
				if (this.showRefundResonPicker == true) {
					this.showRefundResonPicker = false
					this.refundReason = e.value[0].contentName
					this.refundCode = e.value[0].contentCode
				}


			},

			//根据订单编号查询商城订单详情
			getProductOrderDetailsByOrderNo: function() {
				var that = this;
				var uri = app.globalData.config.interfaces
					.URL_PRODUCT_PFMALLORDER_GETPRODUCTORDERDETAILSBYORDERNO; //刷新页面配置
				var params = {
					"orderNo": that.orderNo
				};
				network.get(uri, params, 'form', null, true).then(res => {
					if (res.code == 200) {
						that.data = res.data
						for (var i = 0; i < that.data.pfMallOrderProductsItemList.length; i++) {
							if (that.currentStatus == 1) {
								that.pfMallOrderProductsItemList = that.data.pfMallOrderProductsItemList
							}

							// // 订单最新状态;1待支付、2已支付/待发货、 3,待收货 4,已收货/待评价、5已评价/交易完成、6已取消
							that.currentStatus = that.data.orderNewFlag
							// that.currentStatus = 1
						}
					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
				});
			},
			//取消原因
			getCancelReasonList: function() {
				var that = this
				var url = app.globalData.config.interfaces.URL_DICTIONARY_GETDICTIONARYLIST
				var params = {
					dictionaryCode: 'MALL_ORDER_CANCEL_REASON'
				};
				network.get(url, params, 'json').then(res => {
					if (res.code == 200) {
						that.cancelResonList = [res.data]
					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
				});

			},
			
			//退货原因
			getReturnReasonList: function() {
				var that = this
				var url = app.globalData.config.interfaces.URL_DICTIONARY_GETDICTIONARYLIST
				var params = {
					dictionaryCode: 'REFUND_REASON_GOODS'
				};
				network.get(url, params, 'json').then(res => {
					if (res.code == 200) {
						that.refundResonList = [res.data]
					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
				});

			},
			//查询我的可用优惠券
			getMyUserCouponPageList: function() {
				var that = this;
				var uri = app.globalData.config.interfaces.URL_ACTIVITY_CARD_GETMYUSERCOUPONPAGELIST; //刷新页面配置
				var params = {
					"pageNum": 1,
					"pageSize": 1000,
					"userId": app.globalData.userInfo.id,
					"couponTabFlag": "可用券"
				};
				network.get(uri, params, 'json').then(res => {
					if (res.code == 200) {
						//课包状态
						that.cancelResonList = [res.data.list]

					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
				});
			},

			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				this.isUploading=true
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
				this.isUploading=false
			},
			uploadFilePromise(url) {
				var that = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: app.globalData.config.BASE_URL + app.globalData.config.interfaces
							.URL_ALIYUN_OSS_UPLOAD, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							folder: 'community'
						},
						fileType: "image", //钉钉必填
						header: {
							"token": "Bearer " + app.globalData.token,
							"Authorization": "Bearer " + app.globalData.token,
						},
						success: (uploadFileRes) => {
							setTimeout(() => {
								resolve(JSON.parse(uploadFileRes.data).data)
							}, 1000)

							console.log(JSON.stringify(uploadFileRes))
							//小程序原生请求要先处理statusCode
							if (uploadFileRes.statusCode == 200) {
								var uploadFileData = JSON.parse(uploadFileRes.data);
								console.log("上传成功" + uploadFileRes.data);
								if (uploadFileData.code == 200) {
									that.picURL = uploadFileData.data
								} else {
									dialog.toast("上传失败" + uploadFileData.message)
								}
							} else {
								dialog.toast("上传失败，请重试")
								uploadTask.abort()
							}
						}
					});
				})
			},






		}
	}
</script>
<style>
	@import "./mallDetail.css";

	.empty {
		height: 100%;
		width: 100%;

	}

	.empty .img-box {
		width: 388rpx;
		position: relative;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
	}

	.empty .img-box image {
		width: 100%;
	}

	.empty .text {
		text-align: center;
		font-size: 28rpx;
		line-height: 40rpx;
		color: #ADB0AE;
	}

	.u-nav-slot {
		display: flex;
		flex-direction: row;
	}
</style>
